﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using ConcreteMappersAndObjects.DomainObjects;
using  DataMappers;
using System.Data;


namespace ConcreteMappersAndObjects.Mappers
{
    public class CourseCUDataMapper : AbstractDataMapper<CourseCUKey, CourseCU>
    {

        private IEntityMapper<int, Course> _courseMapper;
        private IEntityMapper<int, CUnit> _cunitsMapper;

        public void SetCourseDataMapper(IEntityMapper<int, Course> mapper)
        {
            _courseMapper = mapper;
        }
        public void SetCUnitDataMapper(IEntityMapper<int, CUnit> mapper)
        {
            _cunitsMapper = mapper;
        }
        protected override CourseCUKey GetKeyFrom(SqlDataReader sdr)
        {
            return new CourseCUKey(sdr.GetInt32(1) ,sdr.GetInt32(2));
        }

        protected override string DoGetAllStatement()
        {
            return "SELECT [CUnitID] , [CourseID] FROM [ExaminationTimetablingDB].[dbo].[CourseCU]";
        }

        protected override string DoGetFindStatement()
        {
            return DoGetAllStatement() + " WHERE [CUnitID] = @CUnitID and [CourseID] = @CourseID";
        }

        protected override void BindFindStatement(SqlCommand cmd, CourseCUKey key)
        {
            cmd.Parameters.AddWithValue("@CUnitID", key.CUnitKey);
            cmd.Parameters.AddWithValue("@CourseID" , key.CourseKey);
        }

        protected override CourseCU DoLoad(SqlDataReader sdr)
        {
            //TODO: Não estou a ver como isto se faz!
            CourseCUKey key = GetKeyFrom(sdr);
            ConcreteValueHolder<int, CUnit> cunit = new ConcreteValueHolder<int, CUnit>(key.CUnitKey, _cunitsMapper);
            ConcreteValueHolder<int, Course> course = new ConcreteValueHolder<int, Course>(key.CourseKey, _courseMapper);
            return new CourseCU(cunit, course, key);
            // -> return new CourseCU(new CUnit(), sdr.GetInt32(1), new CourseCUKey(sdr.GetInt32(1), sdr.GetInt32(2)));
        }

        protected override string GetUpdateCommand()
        {
            throw new NotImplementedException();
        }

        protected override void DoBindUpdate(SqlCommand cmd, CourseCU t)
        {
            throw new NotImplementedException();
        }

        protected override string GetInsertCommand()
        {
            return "insert into CourseCU (CUnitID,CourseID) values (@CUnitID, @CourseID)";
        }

        protected override void DoBindInsert(SqlCommand cmd, CourseCU t)
        {
            //TODO TRY CATCH
            cmd.Parameters.Add("@CUnitID", SqlDbType.Int).Value = t.ID.CUnitKey;
            cmd.Parameters.Add("@CourseID", SqlDbType.Int).Value = t.ID.CourseKey;
        }
        
        protected override string GetRemoveCommand()
        {
            return "delete from CourseCU where CUnitID = @CUnitID and CourseID = @CourseID";
        }

        protected override void DoBindRemove(SqlCommand cmd, CourseCU t)
        {
            cmd.Parameters.Add("@CUnitID", SqlDbType.Int).Value = t.ID.CUnitKey;
            cmd.Parameters.Add("@CourseID", SqlDbType.Int).Value = t.ID.CourseKey;
        }

        protected override void FinishUpdateObject(CourseCU t)
        {
            
        }

        protected override void FinishNewObject(CourseCU t, object id)
        {
            t.ID = new CourseCUKey(t.CUnit.ID, t.Course.ID);
        }

        protected override void FinishDeleteObject(CourseCU t)
        {
            
        }
    }
}
